<template>
	<om-formsub-outer :index="index" :checkedit="checkedit" :deleteBtn="false">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	const app = getApp();
	import {buildSubFormData, subOnLoad, changeEditor,configSelectData,hasValue} from '@/static/js/formUtil.js';
	import {errorModal,ignoreHtml,ptFloat,minusF,getDateStr} from '@/static/js/common.js';
	import moment from '@/static/js/moment.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				posId:"",
				gradeId:'',
				isOk:false,
				isChange:false,
				userId:'',
				x:0,
				x1:0
			})
		},
		methods:{
			verifyUserSalary(newValue){
				let _that = this
				 return _that.om.request(
					'/common/verifyUserSalary.do?newDate=' + newValue + '&userId=' + _that.userId,
				 )
			},
			strToDate(val){
			    // 转换日期格式
			    let str = val.replace(/-/g, '/');
			    // 创建日期对象
			    return new Date(str);
			},
			async onSelectDate(val){
				let _that = this
				_that.$refs.autoform.formdata.afterAdjust = val
				_that.$refs.autoform.formdata.afterAdjustId = val
				if (_that.userId) {
					let data = await _that.verifyUserSalary(val)
					if (data.succeed && data.value) {
						_that.x = 0;
						confirmModal(_that,'提示信息','当前选择人员从' + data.value + '没有薪资信息，是否将薪资调整生效日期设置为' + data.value, function () {
							_that.$refs.autoform.formdata.afterAdjust = data.value
							_that.$refs.autoform.formdata.afterAdjustId = data.value
						}, function () {
						})
					}
				}
				if (_that.x) {
					let now = new Date();
					let d = _that.strToDate(val);
					let d1 = new Date(now.getFullYear(), now.getMonth(), 1);
					let d2 = new Date(now.getFullYear(), now.getMonth() + 2, 0);
					if (_that.x1) {
						d1 = new Date(now.getFullYear(), now.getMonth() - 1, _that.x1 + 1);
						d2 = new Date(now.getFullYear(), now.getMonth() + 1, _that.x1);
					}
					if (d.getTime() < d1.getTime() || d.getTime() > d2.getTime()) {
						if (_that.x1) {
							let str1 = now.getFullYear() + "-" + now.getMonth() + "-" + (_that.x1 + 1);
							let str2 = now.getFullYear() + "-" + (now.getMonth() + 2) + "-" + _that.x1;
							errorModal(_that,'提示信息','管控参数约束了“调薪生效日期”只能选择' + str1 + '到' + str2 + '的日期，请重新选择！', function () {
								_that.$refs.autoform.clear("title_salary","afterAdjustId")
							});
						} else {
							errorModal(_that,'提示信息','管控参数约束了“调薪生效日期”只能选择当月或下月的日期，请重新选择！', function () {
								_that.$refs.autoform.clear("title_salary","afterAdjustId")
							});
						}
					}
				}
			},
			getInfo(){
				let _that = this
				return new Promise((resolve,reject)=>{
					uni.request({
						url:app.globalData.url+"salaryStandardList/getSalaryInfo.do",
						data:{
							gradeId: _that.gradeId,
							salaryStandard:_that.$refs.autoform.formdata.afterAdjust
						},
						method:"get",
						success:(res)=>{
								for(let index in res.data.value){
									if(_that.$refs.autoform.formdata.salaryName == index){
										if(_that.$refs.autoform.formdata.salaryName === "salary3"){
											_that.$refs.autoform.formdata.afterAdjust = res.data.value[index]
											_that.$refs.autoform.formdata.afterAdjustId = res.data.value[index]
											_that.$refs.autoform.formdata.salary = res.data.value[index]
										}else if(_that.$refs.autoform.formdata.salaryName === "salary2"){
											if(res.data.value['salary2Rate']){
												_that.$refs.autoform.formdata.salary = res.data.value[index]+"("+"a*"+(res.data.value['salary3Rate'])+")"
											}else{
												_that.$refs.autoform.formdata.salary = res.data.value[index]
											}
										}else if(_that.$refs.autoform.formdata.salaryName == "salary4"){
											_that.$refs.autoform.formdata.afterAdjust = res.data.value[index]
											_that.$refs.autoform.formdata.afterAdjustId = res.data.value[index]
											if(!res.data.value['salary4Rate']){
												_that.$refs.autoform.formdata.salary = res.data.value[index]
											}else{
												_that.$refs.autoform.formdata.salary = res.data.value[index]+"("+"a*"+(res.data.value['salary4Rate'])+")"
											}
										}else if(_that.$refs.autoform.formdata.salaryName != "gradeName"){
											_that.$refs.autoform.formdata.afterAdjust = res.data.value[index]
											_that.$refs.autoform.formdata.afterAdjustId = res.data.value[index]
										}
									}
							}
							resolve(true)
						}
					})
				})
			},
			async verifySubConfirm(){
				let _that = this
				if(_that.$refs.autoform.formdata.salaryName == "salary1"){
						if(Number(_that.$refs.autoform.formdata.afterAdjustId) == Number(_that.$refs.autoform.formdata.beforeAdjust)){
							return true
						}else{
							let ret = await _that.getInfo()
							return ret
						}
					}else{
						return true
				}
			},
			salaryInput(val){
				let _that = this
				for(let item of _that.datas){
					if(item.salaryName =="levelSalaryRange"){
						if(Number(val)>item.max){
							_that.$refs.autoform.formdata.afterAdjust = item.max
							_that.$refs.autoform.formdata.afterAdjustId = item.max
						}else{
							_that.$refs.autoform.formdata.afterAdjust = val
							_that.$refs.autoform.formdata.afterAdjustId = val
						}
					}else{
						_that.$refs.autoform.formdata.afterAdjust = val
						_that.$refs.autoform.formdata.afterAdjustId = val
					}
				}
			},
			onSelectAfterAdjust(item){
				let _that = this
				if(_that.datas[_that.index].salaryName== 'gradeName'){
					for(let item of _that.datas){
						if(item.salaryName == "levelName"){
							item.afterAdjustId = ''
							item.afterAdjust = ''
						}
					}
					_that.om.request(
						"salaryStandardList/getSalaryRange",
						{
							gradeId:item.id,
							levelId:''
						}
					).then(res=>{
						for(let item of _that.datas){
							if(item.salaryName == "levelSalaryRange"){
								item.afterAdjust = res.value.range
								item.max = res.value.max
							}
						}
					}) 
				}
				if(_that.datas[_that.index].salaryName == 'levelName'){
					_that.om.request(
						"salaryStandardList/getSalaryRange",
						{
							gradeId:_that.gradeId,
							levelId:item.id
						}
					).then(res=>{
						for(let item of _that.datas){
							if(item.salaryName == "levelSalaryRange"){
								item.afterAdjust = res.value.range
								item.max = res.value.max
							}
						}
					})
				}
			},
			initLoad(){
				let _that = this
				let pro1 = _that.om.request("tempInstAdjust/getSelfSalaryLevel",{posId:_that.posId?_that.posId:''})
				let pro2 = _that.om.request("salaryStandardList/getLevelListByGradeId",{gradeName:_that.gradeId})
				let arr = [pro1,pro2]
				if(_that.formdata.salaryName == "salaryEffectiveDate"){
					let pro3 = _that.om.request("dataDict/getIntByCode",{code:'DD014009001'})
					let pro4 = _that.om.request("dataDict/getIntByCode",{code:'DD020010001'})
					arr.push(pro3,pro4)
				}
				Promise.all(arr).then(res=>{
						switch(_that.$refs.autoform.formdata.salaryName){
							case "gradeName": 
								_that.$refs.autoform.changeEditor("title_salary","afterAdjustId","type","selectbox")
								_that.$refs.autoform.changeEditor("title_salary","afterAdjustId","data",configSelectData(res[0],"id","dicname"))
								_that.$refs.autoform.initSelectIndex("title_salary","afterAdjustId")
								_that.$refs.autoform.changeEditor("title_salary","afterAdjustId","onSelect","onSelectAfterAdjust")
							break;
							case "levelName":
							_that.$refs.autoform.changeEditor("title_salary","afterAdjustId","data",configSelectData(res[1],"id","dicname"))
								_that.$refs.autoform.changeEditor("title_salary","afterAdjustId","type","selectbox")
								_that.$refs.autoform.initSelectIndex("title_salary","afterAdjustId")
								_that.$refs.autoform.changeEditor("title_salary","afterAdjustId","onSelect","onSelectAfterAdjust")
							break;
							case "salary1": case "salary2": case "salary3": case "salary4": case "salary5": case "salary6":case "salary7":case "salary8":case "salary23": 
								_that.$refs.autoform.changeEditor("title_salary","afterAdjustId","type","numberbox")
								_that.$refs.autoform.changeEditor("title_salary","afterAdjustId","prompt","请输入")
								_that.$refs.autoform.changeEditor("title_salary","afterAdjustId","onSelect","salaryInput")
								if(_that.datas[_that.index].afterAdjust === '0'){
									_that.$refs.autoform.formdata.afterAdjustId = ''
								}else{
								}
							case "salary10":case "salary11":case "salary12":case "salary13":case "salary14":case "salary15":case "salary16":case "salary17":case "salary18":case "salary19":
							case "salary20":case "salary21":case "salary22":case "salary24":case "salary25":case "salary26":case "salary27":case "salary28":case "salary29":
							case "salary30":case "salary31":case "salary32":case "salary33":case "salary34":case "salary35":case "salary36":case "salary37":case "salary38":case"salary39":
								_that.$refs.autoform.changeEditor("title_salary","afterAdjustId","type","numberbox")
								_that.$refs.autoform.changeEditor("title_salary","afterAdjustId","prompt","请输入")
								_that.$refs.autoform.changeEditor("title_salary","afterAdjustId","onSelect","salaryInput")
								if(_that.datas[_that.index].afterAdjust === '0' || _that.datas[_that.index].afterAdjust === null){
									_that.$refs.autoform.formdata.afterAdjustId = ''
								}else{
								}
							break;
							case "salaryEffectiveDate":
								let editor  = _that.$refs.autoform.getEditor("title_salary","afterAdjustId")
								_that.$refs.autoform.changeEditor("title_salary","afterAdjustId","type","datebox")
								_that.$refs.autoform.changeEditor("title_salary","afterAdjustId","prompt","请输入")
								_that.$refs.autoform.changeEditor("title_salary","afterAdjustId","onSelect","onSelectDate")
								_that.x =  res[2].value
								_that.x1 = res[3].value
							break;
							default:
							break;
						}
				})
			}
		},
		onShow() {
			
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				_that.posId = e.newPosId
				_that.userId = e.userId
				_that.isChange = (e.changeTextArea=='true'?true:false)
			if(e.configDataCacheKey){
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
							_that.autoformdata = res.data
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							if(_that.datas[_that.index].salaryName == 'salary1' || _that.datas[_that.index].salaryName == 'salary2' || _that.datas[_that.index].salaryName == 'salary3' || _that.datas[_that.index].salaryName == 'salary4'){
							}else{
								changeEditor("title_salary","salary","type","",_that.autoformdata)
							}
							for(let item of _that.datas){
								if(item.salaryName == "gradeName" && item.afterAdjustId){
									_that.gradeId = item.afterAdjustId
								}else if(item.salaryName == "adjust13"){
									_that.gradeId = item.afterAdjustId
								}
							}
								_that.initLoad()
							}
						}
					})
				}
			})
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                         
